package com.atguigu.tingshu.album.job;

import cn.hutool.core.collection.CollectionUtil;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qcloud.vod.common.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ClassName: ReviewResultTask
 * Package: com.atguigu.tingshu.album.job
 * Description:
 *
 * @Author 咚咚小圆帽
 * @Create 2025/6/27 18:06
 * @Version 1.0
 */
@Slf4j
@Component
public class ReviewResultTask {

    @Autowired
    private TrackInfoMapper trackInfoMapper;

    @Autowired
    private VodService vodService;

    /*
     cron表达式：秒 分 时 日 月 周 年
     @Scheduled(cron = "* * * * * ?")
     定时获取音视频的审核结果，根据审核结果更新声音审核状态
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void ReviewResultJob() {
        log.info("[专辑服务]定时审核音频结果开始...");
        //1.查询审核状态为审核中的声音列表
        LambdaQueryWrapper<TrackInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrackInfo::getStatus, SystemConstant.TRACK_STATUS_REVIEWING)
                .select(TrackInfo::getId, TrackInfo::getReviewTaskId)
                //进行限流处理，每次调用最多只处理100个数据
                .last("limit 100");
        List<TrackInfo> trackInfoList = trackInfoMapper.selectList(queryWrapper);

        //2.遍历列表，根据审核任务id调用云点播平台，获取审核结果
        if (CollectionUtil.isNotEmpty(trackInfoList)) {
            for (TrackInfo trackInfo : trackInfoList) {
                //3.根据审核结果更新声音表审核状态
                //3.1调用云点播平台，获取审核结果
                String result = vodService.getReviewResult(trackInfo.getReviewTaskId());

                //3.2根据审核结果更新声音表审核状态
                if (StringUtil.isNotEmpty(result)) {
                    if ("pass".equals(result)) {
                        trackInfo.setStatus(SystemConstant.TRACK_STATUS_PASS);
                    } else if ("block".equals(result)) {
                        trackInfo.setStatus(SystemConstant.TRACK_STATUS_NO_PASS);
                    } else if ("review".equals(result)) {
                        trackInfo.setStatus(SystemConstant.TRACK_STATUS_MANUAL);
                    }

                    trackInfoMapper.updateById(trackInfo);
                }

            }
        }


    }


}
